import com.mysql.cj.jdbc.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCUpdate {
    public static void main(String[] args) throws SQLException {
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java104?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("123456");

        Connection connection = dataSource.getConnection();

        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要修改的学号:");
        int sum = scan.nextInt();
        System.out.println("请输入要修改的姓名:");
        String name = scan.next();
        System.out.println("请输入要修改的条件:");
        int eat = scan.nextInt();
        String sql = "update student set id = ?,name = ? where id = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1,sum);
        preparedStatement.setString(2,name);
        preparedStatement.setInt(3,eat);
        preparedStatement.executeUpdate();

        preparedStatement.close();
        connection.close();
    }
}
